const http = require('http')
const requestConfig = {
  hostname: 'localhost',
  port: 3000,
  path: '/',
  method: 'POST'
}
const request = http.request(requestConfig)
request.on('response', function (res) {
  res.on('data', function (chunk) {
    console.log('客户端接收到数据: ' + chunk);
  });
})
// request.on('response', (res) => {
//   console.log('状态码:', res.statusCode);
//   console.log('响应头', JSON.stringify(res.headers));
//   res.setEncoding('utf8')
//   res.on('data', (dunk) => {
//     console.log(dunk);
//   })
// })
request.write('你好再见')
request.on('data', (data) => {
  console.log(data);
})
request.on('socket', (socket) => {
  // socket.setTimeout(1000) //socket 超时
  // socket.on('timeout', () => {
  // })
  console.log();
})
request.setTimeout(10000, () => {
  request.abort()
})

request.on('error', (err) => {
  if (err.code == 'ECONNRESET') {
    console.log(' sokect 端口超时');
  } else {
    console.log('请求错误,错误代码：', err.code);
  }
})